//
//  LoginAndSignUpVC.m
//  NoNeedle
//
//  Created by Ming Tian on 2017/3/8.
//  Copyright © 2017年 Ming Tian. All rights reserved.
//

#import "LoginAndSignUpVC.h"
#import "FirstForgetViewController.h"


//#import <NIMSDK/NIMSDK.h>
//#import <NIMAVChat/NIMAVChat.h>


#define LOGIN_BTN  100
#define SIGNUP_BTN 101

#define USERNAME   @"username"
#define USERPASS   @"passwd"

@interface LoginAndSignUpVC ()

@end

@implementation LoginAndSignUpVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - 界面布局
- (void)createUI {
    // 导航栏
    self.navigationController.navigationBar.translucent = YES;
    // 头部背景
    CGFloat headerViewRate = 325.0f/250.0f;
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -20, screenW, screenW/headerViewRate)];
    self.headerImageView.userInteractionEnabled = YES;
    self.headerImageView.image = [UIImage imageNamed:@"img_login_bg"];
    self.headerImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.headerImageView.clipsToBounds = YES;
    [self.view addSubview:self.headerImageView];
    // 头部返回按钮
    self.backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 42, 42)];
    [self.backButton setImage:[UIImage imageNamed:@"icon_nav_renter"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    // 头部登录选项
    self.loginSelectButton = [[UIButton alloc]init];
    self.loginSelectButton.tag = LOGIN_BTN;
    self.loginSelectButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.loginSelectButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginSelectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.headerImageView addSubview:self.loginSelectButton];
    [self.loginSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(50);
        make.centerX.equalTo(self.headerImageView).multipliedBy(0.5);
        make.bottom.equalTo(self.headerImageView).offset(-10);
    }];
    // 头部注册选项
    self.signUpSelectButton = [[UIButton alloc]init];
    self.signUpSelectButton.tag = SIGNUP_BTN;
    self.signUpSelectButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.signUpSelectButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.signUpSelectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.headerImageView addSubview:self.signUpSelectButton];
    [self.signUpSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
        make.centerX.equalTo(self.headerImageView).multipliedBy(1.5);
        make.bottom.equalTo(self.headerImageView).offset(-10);
    }];
    // 登录注册选项指示器
    self.indicatorImageView = [[UIImageView alloc]init];
    self.indicatorImageView.image = [UIImage imageNamed:@"icon_login_triangle"];
    [self.headerImageView addSubview:self.indicatorImageView];
    [self.indicatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.loginSelectButton);
        make.bottom.equalTo(self.headerImageView);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(15);
    }];
  [self loadViewIfNeeded];
    
    // 滑动ScrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.headerImageView.frame.size.height-20, screenW, screenH-self.headerImageView.frame.size.height + 20)];
    self.scrollView.contentSize = CGSizeMake(screenW * 2, screenH-self.headerImageView.frame.size.height);
    self.scrollView.decelerationRate = 0.5;
    self.scrollView.bounces = YES;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    // 登录界面
    self.loginView = [[LoginView alloc]init];
    [self.loginView setFrame:CGRectMake(0, 0, screenW, self.scrollView.frame.size.height)];
    [self.scrollView addSubview:self.loginView];
    if(USER_DEFAULTGet(USERNAME)) {
        self.loginView.usernameTextField.text = USER_DEFAULTGet(USERNAME);
    }
    
    // 注册界面
    self.signupView = [[SignUpView alloc]init];
    [self.signupView setFrame:CGRectMake(screenW, 0, screenW, self.scrollView.frame.size.height)];
    [self.scrollView addSubview:self.signupView];
    
    // 代理绑定
    self.signupView.hintTextView.delegate = self;
    
    // 按钮事件绑定
    [self.loginSelectButton addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [self.signUpSelectButton addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.forgetPasswordButton addTarget:self action:@selector(forgetPasswordAction) forControlEvents:UIControlEventTouchUpInside];
    [self.signupView.verifyButton addTarget:self action:@selector(verifyCodeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.signupView.signupButton addTarget:self action:@selector(signupAction) forControlEvents:UIControlEventTouchUpInside];
    [self.signupView.checkboxButton addTarget:self action:@selector(userAgreementAction) forControlEvents:UIControlEventTouchUpInside];
    // 地区选择
//    [self.signupView.addressChoose addTarget:self action:@selector(addressAction) forControlEvents:UIControlEventTouchUpInside];
//
//    // picker
//    self.pickerView = [[RegionPicker alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
//    self.pickerView.pickerView.dataSource = self;
//    self.pickerView.pickerView.delegate = self;
//    [self.pickerView.confirmButton addTarget:self action:@selector(regionSelectAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.pickerView setHidden:YES];
//    [self.view addSubview:self.pickerView];
}

#pragma mark - 按钮响应
// 切换登录注册
- (void)changePage:(UIButton *)btn {
    if(btn.tag == LOGIN_BTN) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if(btn.tag == SIGNUP_BTN) {
        [self.scrollView setContentOffset:CGPointMake(screenW, 0) animated:YES];
    }
}
// 返回按钮响应
- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

// 忘记密码响应
- (void)forgetPasswordAction {
    FirstForgetViewController *forgetPasswordVC = [[FirstForgetViewController alloc]init];
    forgetPasswordVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forgetPasswordVC animated:YES];
}
// 同意用户协议响应
- (void)userAgreementAction {
    self.signupView.checkboxButton.selected = self.signupView.checkboxButton.isSelected ? NO : YES;
}
// 登录响应
- (void)loginAction {
    if(self.loginView.usernameTextField.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入手机号" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.loginView.passwordTextField.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入密码" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    
    NSDictionary *parameter = @{@"userAccount":self.loginView.usernameTextField.text, @"userPassword":self.loginView.passwordTextField.text};
    MBProgressHUD *hud = [MBProgressHUD showMessag:@"正在登录" withDelay:1 toView:self.view withMode:MBProgressHUDModeIndeterminate hideWithDelay:0];
    [PostNetWorking postNetWorkingStr:[SERVER_HOST stringByAppendingString:user_login_api] andparameters:parameter Success:^(id responseObject) {
        hud.isContinue = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                //成功
                [MBProgressHUD showMessag:@"登录成功" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
                
                USER_DEFAULTSet(@"OK", @"enter");
                USER_DEFAULTSet(@"OK", @"tokenVerify");
                // 讲用户数据存储本地
                USER_DEFAULTSet(responseObject[@"datas"], @"user");
                USER_DEFAULTSet(responseObject[@"datas"][@"mid"], @"mid");
                USER_DEFAULTSet(responseObject[@"datas"][@"token"], @"token");
                USER_DEFAULTSet(responseObject[@"datas"][@"username"], @"username");
                USER_DEFAULTSet(responseObject[@"datas"][@"user_token"], @"user_token");
                USER_DEFAULTSet(self.loginView.usernameTextField.text, USERNAME);
                USER_DEFAULTSet(self.loginView.passwordTextField.text, USERPASS);

                // 绑定极光推送别名
                [[NSNotificationCenter defaultCenter] postNotificationName:@"bindAlia" object:@"YES"];
                
                                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
//                    if ([responseObject[@"datas"][@"pay_password"] isEqualToString:@"0"]) {
//
//
//
//                        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有设置支付密码，现在去设置" preferredStyle:UIAlertControllerStyleAlert];
//
//                        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                            NSLog(@"没有支付密码");
//                            SafetyCenterVC *satetyVC = [[SafetyCenterVC alloc]init];
//                            satetyVC.isLoging = @"isLoging";
//                            satetyVC.hidesBottomBarWhenPushed = YES;
//                            [self.navigationController pushViewController:satetyVC animated:YES];
//                        }];
//                        [alert addAction:action1];
//                         [self presentViewController:alert animated:YES completion:nil];
//
//                    }else {
                       [self.navigationController popViewControllerAnimated:YES];
//                    }
                });
            }
                break;
            case 210:
            {
                //逻辑错误
                [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
                break;
            default:
                break;
        }
    } failed:^(NSError *error) {
        hud.isContinue = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}
// 注册发送验证码响应
- (void)verifyCodeAction {
    if(self.signupView.usernameTextField.text.length != 11) {
        [MBProgressHUD showMessag:@"请输入正确的11位手机号" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    
    [self openCountdown]; // 开启倒计时
    NSDictionary *parameter = @{@"username":self.signupView.usernameTextField.text,
                                @"type":@"register"
                                };
    [PostNetWorking postNetWorkingStr:[SERVER_HOST stringByAppendingString:user_signup_code_api] andparameters:parameter Success:^(id responseObject) {
        if([responseObject[@"code"]integerValue] == 200) {
            [MBProgressHUD showMessag:@"验证码已发送" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        }
        if([responseObject[@"code"]integerValue] == 210) {
            [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}
// 注册响应
- (void)signupAction {
    if(self.signupView.usernameTextField.text.length != 11) {
        [MBProgressHUD showMessag:@"请输入正确的11位手机号" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.signupView.verifyTextField.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入验证码" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.signupView.passwordTextField.text.length == 0) {
        [MBProgressHUD showMessag:@"请输入密码" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.signupView.passwordTextField.text.length < 6 || self.signupView.passwordTextField.text.length > 18) {
        [MBProgressHUD showMessag:@"密码长度为6-18位" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(![self.signupView.passwordTextField.text isEqualToString:self.signupView.confirmPasswordTextField.text]) {
        [MBProgressHUD showMessag:@"两次密码不一致" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(self.signupView.addressTextField.text.length==0) {
        [MBProgressHUD showMessag:@"推荐人必填" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
    if(!self.signupView.checkboxButton.isSelected) {
        [MBProgressHUD showMessag:@"请阅读并同意用户协议" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        return;
    }
//    if (_country_code.length==0) {
//        _country_code = @"110101";
//    }
    
    NSDictionary *parameter = @{@"username":self.signupView.usernameTextField.text,
                                @"password":self.signupView.passwordTextField.text,
                                @"code":self.signupView.verifyTextField.text,
                                @"p_phone":self.signupView.addressTextField.text
                                 };
    MBProgressHUD *hud = [MBProgressHUD showMessag:@"正在注册" withDelay:1 toView:self.view withMode:MBProgressHUDModeIndeterminate hideWithDelay:0];
    [PostNetWorking postNetWorkingStr:[SERVER_HOST stringByAppendingString:user_signup_api] andparameters:parameter Success:^(id responseObject) {
        hud.isContinue = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                //成功
                [MBProgressHUD showMessag:@"注册成功" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:3];
                self.loginView.usernameTextField.text = self.signupView.usernameTextField.text;
                self.signupView.usernameTextField.text = @"";
                self.signupView.verifyTextField.text = @"";
                self.signupView.passwordTextField.text = @"";
                self.signupView.confirmPasswordTextField.text = @"";
                self.signupView.addressTextField.text = @"";
                [self.signupView.checkboxButton setSelected:NO];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                     [self changePage:self.loginSelectButton];
                });
               
            }
                break;
            case 210:
            {
                //逻辑错误
                [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
                break;
            default:
                break;
        }
    } failed:^(NSError *error) {
        hud.isContinue = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}

#pragma mark - 验证码倒计时
-(void)openCountdown{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.signupView.verifyButton setTitle:@"获取" forState:UIControlStateNormal];
                [self.signupView.verifyButton setTitleColor:NavBackColor forState:UIControlStateNormal];
                self.signupView.verifyButton.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.signupView.verifyButton setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:UIControlStateNormal];
                [self.signupView.verifyButton setTitleColor:NavBackColor forState:UIControlStateNormal];
                self.signupView.verifyButton.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint center = self.indicatorImageView.center;
    CGFloat centerX = (scrollView.contentOffset.x / 2) + screenW * 0.25;
    [self.indicatorImageView setCenter:CGPointMake(centerX, center.y)];
}
#pragma mark - UITextview delegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange {
    UserAgreementVC *userAgreementVC = [[UserAgreementVC alloc]init];
    userAgreementVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userAgreementVC animated:YES];
    return NO;
}

#pragma mark - 选择地区点击事件
-(void)addressAction{
    //区域
//    if(!self.province) {
//        NSString * jsonPath = [[NSBundle mainBundle]pathForResource:@"region" ofType:@"json"];
////        NSString *jsonPath =USER_DEFAULTGet(@"RegionPath");
//        NSData * jsonData = [[NSData alloc]initWithContentsOfFile:jsonPath];
//        self.province = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
//        self.city = self.province[0][@"city"];
//        self.country = self.city[0][@"country"];
//        self.mem_province = self.province[0][@"province_code"];
//        self.mem_city = self.city[0][@"city_code"];
//        self.mem_country = self.city[0][@"country_code"];
//        self.selectedRegion = [NSString stringWithFormat:@"%@%@%@",self.province[0][@"province_name"], self.city[0][@"city_name"], self.country[0][@"country_name"]];
//    }
//    [self.pickerView setHidden:NO];
//    [self.pickerView.pickerView reloadAllComponents];
    
}
// 区域选择确定
//- (void)regionSelectAction {
//    [self.pickerView setHidden:YES];
//    self.region = self.selectedRegion;
//    self.province_code = self.mem_province;
//    self.city_code = self.mem_city;
//    self.country_code = self.mem_country;
//
//    self.signupView.addressTextField.text = self.region;
//}

#pragma mark - UIPickerView datasource
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
//    return 3;
//}
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//    switch (component) {
//        case 0:
//        {
//            if(![self.province isKindOfClass:[NSNull class]]) {
//                return self.province.count;
//            }else {
//                return 0;
//            }
//        }
//            break;
//        case 1:
//        {
//            if(![self.city isKindOfClass:[NSNull class]]) {
//                return self.city.count;
//            }else {
//                return 0;
//            }
//        }
//            break;
//        case 2:
//        {
//            if(![self.country isKindOfClass:[NSNull class]]) {
//                return self.country.count;
//            }else {
//                return 0;
//            }
//        }
//            break;
//        default:
//            break;
//    }
//    return 0;
//}
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    switch (component) {
//
//        case 0:
//        {
//            // 省份
//            return self.province[row][@"simple_province_name"];
//        }
//            break;
//        case 1:
//        {
//            // 城市
//            return self.city[row][@"simple_city_name"];
//        }
//            break;
//        case 2:
//        {
//            // 乡镇、街道
//            return self.country[row][@"simple_country_name"];
//        }
//            break;
//        default:
//            break;
//    }
//    return nil;
//}

#pragma mark - UIPickerView delegate
//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    switch (component) {
//        case 0:
//        {
//            // 省份
//            self.city = self.province[row][@"city"];
//            self.country = self.city[0][@"country"];
//            [pickerView selectRow:0 inComponent:1 animated:NO];
//            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:1];
//        }
//            break;
//        case 1:
//        {
//            // 城市
//            self.country = self.city[row][@"country"];
//            [pickerView selectRow:0 inComponent:2 animated:NO];
//            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:2];
//        }
//            break;
//        case 2:
//        {
//            // 乡镇、街道
//        }
//            break;
//        default:
//            break;
//    }
//
//
//    self.mem_province = self.province[[pickerView selectedRowInComponent:0]][@"province_code"];
//    self.selectedRegion = self.province[[pickerView selectedRowInComponent:0]][@"province_name"];
//
//    self.mem_city = self.city[[pickerView selectedRowInComponent:1]][@"city_code"];
//    self.selectedRegion = [self.selectedRegion stringByAppendingString:self.city[[pickerView selectedRowInComponent:1]][@"city_name"]];
//
//    if([self.country isKindOfClass:[NSArray class]]) {
//        self.mem_country = self.country[[pickerView selectedRowInComponent:2]][@"country_code"];
//        self.selectedRegion = [self.selectedRegion stringByAppendingString:self.country[[pickerView selectedRowInComponent:2]][@"country_name"]];
//    }else {
//        self.mem_country = @"110101";
//    }
//
//    [pickerView reloadAllComponents];
//}
@end
